package com.frcdi.qms.equipment.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.frcdi.qms.common.constant.UserConstants;
import com.frcdi.qms.common.exception.ServiceException;
import com.frcdi.qms.common.utils.StringUtils;
import com.frcdi.qms.equipment.mapper.EquipmentBaseMapper;
import com.frcdi.qms.masterdata.domain.MethodExecute;
import com.frcdi.qms.masterdata.domain.MethodProject;
import com.frcdi.qms.masterdata.domain.MethodStep;
import com.frcdi.qms.masterdata.mapper.MethodExecuteMapper;
import com.frcdi.qms.masterdata.mapper.MethodProjectMapper;
import com.frcdi.qms.masterdata.mapper.MethodStepMapper;
import com.frcdi.qms.equipment.domain.EquipmentValidation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.frcdi.qms.equipment.mapper.EquipmentValidationMapper;
import com.frcdi.qms.equipment.service.IEquipmentValidationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备验证记录Service业务层处理
 *
 * @author cell
 * @date 2023-12-19
 */
@Service
public class EquipmentValidationServiceImpl implements IEquipmentValidationService
{
    @Autowired
    private EquipmentValidationMapper equipmentValidationMapper;
    @Autowired
    private EquipmentBaseMapper equipmentBaseMapper;
    @Autowired
    private MethodStepMapper methodStepMapper;
    @Autowired
    private MethodProjectMapper methodProjectMapper;
    @Autowired
    private MethodExecuteMapper methodExecuteMapper;

    /**
     * 查询设备验证记录
     *
     * @param validationId 设备验证记录主键
     * @return 设备验证记录
     */
    @Override
    public EquipmentValidation selectEquipmentValidationByValidationId(Long validationId)
    {
        return equipmentValidationMapper.selectEquipmentValidationByValidationId(validationId);
    }

    /**
     * 查询设备验证记录列表
     *
     * @param equipmentValidation 设备验证记录
     * @return 设备验证记录
     */
    @Override
    public List<EquipmentValidation> selectEquipmentValidationList(EquipmentValidation equipmentValidation)
    {
        return equipmentValidationMapper.selectEquipmentValidationList(equipmentValidation);
    }

    /**
     * 新增设备验证记录
     *
     * @param equipmentValidation 设备验证记录
     * @return 结果
     */
    @Override
    public int insertEquipmentValidation(EquipmentValidation equipmentValidation)
    {
        return equipmentValidationMapper.insertEquipmentValidation(equipmentValidation);
    }

    /**
     * 修改设备验证记录
     *
     * @param equipmentValidation 设备验证记录
     * @return 结果
     */
    @Override
    public int updateEquipmentValidation(EquipmentValidation equipmentValidation)
    {
        return equipmentValidationMapper.updateEquipmentValidation(equipmentValidation);
    }

    /**
     * 批量删除设备验证记录
     *
     * @param validationIds 需要删除的设备验证记录主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentValidationByValidationIds(Long[] validationIds)
    {
        return equipmentValidationMapper.deleteEquipmentValidationByValidationIds(validationIds);
    }

    /**
     * 删除设备验证记录信息
     *
     * @param validationId 设备验证记录主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentValidationByValidationId(Long validationId)
    {
        return equipmentValidationMapper.deleteEquipmentValidationByValidationId(validationId);
    }

    /**
     * 评估设备验证记录
     *
     * @param validationId 设备验证记录id
     */
    @Transactional
    @Override
    public boolean appraiseEquipmentValidation(Long validationId) {
        // 原始数据
        EquipmentValidation result = equipmentValidationMapper.selectEquipmentValidationByValidationId(validationId);
        // 读取所有方法对应的项目组ID列表
        MethodProject methodProject = new MethodProject();
        methodProject.setMethodId(result.getMethod());
        List<MethodProject> methodProjects = methodProjectMapper.selectMethodProjectList(methodProject);
        // stepList是所有需要执行的步骤
        List<Long> stepList = new ArrayList<>();
        methodProjects.forEach(project -> {
            MethodStep methodStep = new MethodStep();
            methodStep.setProjectId(project.getId());
            stepList.addAll(methodStepMapper.selectMethodStepList(methodStep).stream().map(MethodStep::getStepId).collect(Collectors.toList()));
        });
        // 获取所有已执行的步骤ID列表
        MethodExecute methodExecute = new MethodExecute();
        methodExecute.setValidation(validationId);
        methodExecute.setValidationNumber(result.getValidationNumber());
        List<Long> executeStepList = methodExecuteMapper.selectMethodExecuteList(methodExecute).stream().map(MethodExecute::getStep).collect(Collectors.toList());
        if (executeStepList.containsAll(stepList)){
            // 修改验证单据状态
            EquipmentValidation equipmentValidation = new EquipmentValidation();
            equipmentValidation.setValidationId(validationId);
            equipmentValidation.setStatus(UserConstants.EXECUTION_COMPLETE);
            equipmentValidationMapper.updateEquipmentValidation(equipmentValidation);
            return UserConstants.UNIQUE;
        }
        return UserConstants.NOT_UNIQUE;
    }
}
